package vue.actions;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import DB.Personne;

import modele.Batiment;
import vue.FenetrePrincipale;
import vue.composants.Boutton;

public class dialogIncendie extends JDialog implements ActionListener{
	private FenetrePrincipale mere;
	private Boutton incendie;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;
	private JTable tablePersonnes;
	
	
	private static final long serialVersionUID = 1L;

	public dialogIncendie(FenetrePrincipale fp)
	{
		super(fp);
		this.setMere(fp);
		this.setBounds(500, 500, 500, 500);
		
		this.incendie = new Boutton("Incendie !");
		this.incendie.addActionListener(this);
		this.getContentPane().add(this.incendie);
		this.creeComboBox();
		this.getContentPane().setLayout(new FlowLayout());
		
		this.setVisible(true);
	}
	
	private void creeTablePersonneComplete()
	{	
		Object[][] valeurTable = new Object[((Batiment)this.comboBox.getSelectedItem()).getPersonnes().size()][1];
		Object[] col = {"Personne"};
		
		int i =0;
		for(Personne p : ((Batiment)this.comboBox.getSelectedItem()).getPersonnes())
		{
			valeurTable[i++][0]=p.getNom()+" "+p.getPrenom();
		}
		
		this.tablePersonnes = new JTable(valeurTable,col);
		JScrollPane sc = new JScrollPane(this.tablePersonnes);
		this.getContentPane().add(sc);
		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void creeComboBox()
	{
		this.comboBox = new JComboBox(this.mere.getControleur().getBatiments().toArray());
		this.getContentPane().add(this.comboBox);
	}
	
	private void incendie()
	{
		this.getMere().getControleur().setAlarme(true);
		
		// trompette
		
		this.creeTablePersonneComplete();
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==e)
		{
			this.incendie();
		}
	}

	/**
	 * @return the mere
	 */
	public FenetrePrincipale getMere() {
		return mere;
	}

	/**
	 * @param mere the mere to set
	 */
	public void setMere(FenetrePrincipale mere) {
		this.mere = mere;
	}
}
